<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/5
 * Time: 上午9:43
 */
class SendController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('道具/货币发送列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
// 		$filter->addSeaOsFilter();
// 		$filter->addOssServer(true)
		$filter->addSeaSpecialFilter(true);
		$filter->addSeaOsFilterAll();
		$filter->addOssServerAll()
		->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
		
		$statusList = SendService::status();
		$statusList = array('' => '所有') + $statusList;
		$filter->addFilter('status', 'select', '状态', array(
				'options'   => $statusList,
				'value'     => key($statusList)
		));
	
		$params = $filter->get();
		$header = SendService::indexHeader();
	    $data   = SendService::indexData($params);
	
		$grid->addGrid($header, $data);
	
		$this->out['filter'] = $filter->show();
		$this->out['grid'] = $grid->show();
	}
	
	public function approveList(){
		$this->setTitle('道具/货币审核列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addSeaSpecialFilter(true);
		$filter->addSeaOsFilterAll();
		$filter->addOssServerAll()
// 		$filter->addSeaOsFilter();
// 		$filter->addOssServer(true)
		->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
		
		$statusList = SendService::status();
		$filter->addFilter('status', 'select', '状态', array(
				'options'   => $statusList,
				'value'     => key($statusList)
		));
		
		$params = $filter->get();
		$header = SendService::approveHeader();
		if(PubFunc::isAjax()){
			$result = SendService::approveData($params);
			 
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'id';
			$params['m'] = PubRequest::R('m');
			$url = PubFunc::ajaxUrl("?r=send/approveList", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'send/approveList'
			));
			 
			$this->out['grid'] = $grid->show();
			$this->out['filter'] = $filter->show();
		}
	}
	
	public function add(){
		$this->setTitle('申请道具/货币发送');	
		//$this->out['serverList'] = ServerService::getOptions();
		$this->out['sendType']      = SendService::sendType();
		
		$osList = PubAuthSession::get('os');
		$this->out['osList']      = $osList;
		
		$this->out['serverList'] = ServerService::getOptions1();
		
		$specialList = SpecialServerService::specialList();
		$this->out['specialList'] = $specialList;
	}
	
	public function doAdd(){
		$result = SendService::doAdd($_POST);
		 
		if($result['state']){
			$this->showMessage('申请成功', PubFunc::buildUrl('/oss/?r=send/index'));
		}else{
			$this->showMessage($result['msg']);
		}
	}
	
	
	
	public function delete(){
		$id = $this->R('id');
	
		$result = SendService::delete($id);
	
		if($result !== false){
			$this->showMessage('删除成功', PubFunc::buildUrl("/oss/?r=send/index"));
		}else{
			$this->showMessage('删除失败');
		}
	}
	
	/**
	 * 批准、驳回
	 */
	public function setStatus(){
		$id = $this->R('id');
		$status = $this->R('status');
		
		$result = SendService::setStatus($id,$status);
		
		$this->out = $result;
		$this->outType = 'json';
	}
	
	/**
	 * 批量批准、驳回
	 */
	public function batchSetStatus(){
		$ids = $this->R('ids');
		$status = $this->R('status');
	
		$result = SendService::batchSetStatus($ids,$status);
	
		$this->out = $result;
		$this->outType = 'json';
	}
	
	/**
	 * 查看审批信息
	 */
	public function getApproval(){
		$this->layout = '';
		$id = PubRequest::R('id');

        $result = SendService::getApproval($id);
        if(!$result){
        	$result = array();
        }
        
        $this->out['list']      = $result;
	}
	
    public function edit(){
        $id = $this->R('id');
        $info = SendService::get($id);

        if(empty($info)) $this->showMessage('找不到该信息');

        $this->setTitle('编辑道具/货币发送 ', $info['server_name']);

//         echo '<pre>';
//         var_dump($info);exit;
        $this->out['info'] = $info;
        
        $this->out['sendType']      = SendService::sendType();
        
        $osList = PubAuthSession::get('os');
        $this->out['osList']      = $osList;
        
        $this->out['serverList'] = ServerService::getOptions1();
       
    }

    public function doEdit(){
        $id = $this->R('id');
        $result = SendService::doEdit($id, $_POST);

        if($result !== false){
            $this->showMessage('编辑成功', PubFunc::buildUrl("/oss/?r=send/index"));
        }else{
            $this->showMessage('编辑失败');
        }
    }

}